package com.gjzt.business.depositOrder.service;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.DateUtil;
import com.gjzt.business.depositOrder.inter.DepositOrderMapper;
import com.gjzt.business.depositOrder.model.DepositOrder;
import com.gjzt.business.utils.Constant;
import com.unionpay.acp.sdk.LogUtil;

import net.sf.json.JSONObject;

@Service
public class DepositOrderService extends BaseService {

	@Autowired
	private DepositOrderMapper depositOrderMapper;

	/**
	 * 查询
	 *
	 * @param reqBean
	 * @return
	 */
	public JSONObject findDepositOrder(ReqBean reqBean) {
		String sql = "SELECT adr.*,ac.CUSTOMER_NAME,ab.BID_TITLE  from  AD_DEPOSIT_ORDER  adr "
				+ "LEFT JOIN AD_CUSTOMER ac on ac.CUSTOMER_ID = adr.CUSTOMER_ID "
				+ "LEFT JOIN AD_BIDDING ab on ab.BID_ID = adr.BID_ID where 1=1 ";
		SqlBuilder sqlBuilder = new SqlBuilder(sql);
		
		if (Constant.QUICK_SEARCH_FALSE.equals(reqBean.getInfo("quickSearch"))) {// 正常查询
			sqlBuilder.addWhereLike2("ac.CUSTOMER_NAME", reqBean.getInfo("customerName"))
			.addWhere("adr.ORDER_STATUS", reqBean.getInfo("orderStatus"));
		}else {
			sqlBuilder.addWhereLike2("adr.ORDER_ID", reqBean.getInfo("orderId"));
		}

		return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
	}

	public static void main(String[] args) {
		String txnAmt = "1";// 交易金额
		double s =Integer.valueOf(txnAmt)*0.01;
		System.out.println(s);
	}
	
	/**
	 * 前台发起支付，生成待支付订单数据
	 * 
	 * @param submitFromData
	 * @param customerId
	 */
	public void saveOrder(String orderId, String txnType, String txnTime, String money, String channelType,
			String currencyCode, String bidId, String customerId, String orderStatus) {
		DepositOrder depositOrder = new DepositOrder();

		depositOrder.setId(intIncrementId("AD_DEPOSIT_ORDER", "ID"));
		depositOrder.setOrderId(orderId);// 订单id
		depositOrder.setTxnType(txnType);// 交易类型
		depositOrder.setTxnTime(txnTime);// 订单发送时间
		depositOrder.setTxnAmt(money);// 交易金额 单位：元
		depositOrder.setChannelType(channelType);// 渠道类型
		depositOrder.setCurrencyCode(currencyCode);// 交易币种
		depositOrder.setBidId(bidId);// 竞价项目id
		depositOrder.setCustomerId(customerId);// 客户id
		depositOrder.setMakeTime(new Date());
		depositOrder.setOrderStatus(orderStatus);// 待支付
		LogUtil.writeLog("订单信息 [orderId:" + orderId + ";txnType:" + txnType + ";txnTime:" + txnTime + ";txnAmt:" + money
				+ ";channelType:" + channelType + ";currencyCode:" + currencyCode + ";bidId:" + bidId + ";customerId:"
				+ customerId + "]");
		depositOrderMapper.insert(depositOrder);
	}

	public DepositOrder getDepositOrder(String orderId) {
		return depositOrderMapper.selectByPrimaryKey(orderId);
	}

	/**
	 * 更新订单状态(后台通知调用)
	 * 
	 * 重要：根据返回码，确定是否操作成功，成功后向数据库插入保证金数据
	 * 
	 * @param valideData
	 */
	public void updateOrder(Map<String, String> valideData, String orderStatus, String orderDesc) {
		String orderId = valideData.get("orderId");// 订单
		String queryId = valideData.get("queryId");// 交易查询流水号
		String settleDate = valideData.get("settleDate");// 清算日期

		DepositOrder depositOrder = depositOrderMapper.selectByPrimaryKey(orderId);
		depositOrder.setQueryId(queryId);
		depositOrder.setSettleDate(settleDate);
		depositOrder.setOrderStatus(orderStatus);
		depositOrder.setOrderDesc(orderDesc);
		depositOrder.setModifyTime(new Date());
		depositOrderMapper.updateByOrderId(depositOrder);
	}
	
	/**
	 * 更新下单时间
	 * @param orderId
	 */
	public void updateOrderMakeTime(String orderId,String txnTime) {
		DepositOrder depositOrder = depositOrderMapper.selectByPrimaryKey(orderId);
		depositOrder.setMakeTime(new Date());
		depositOrder.setTxnTime(txnTime);
		depositOrderMapper.updateByOrderId(depositOrder);
	}

	/**
	 * 更新订单状态时间
	 * @param orderId
	 */
	public void updateOrderStatus(String orderId,String orderStatus,String orderDesc) {
		DepositOrder depositOrder = depositOrderMapper.selectByPrimaryKey(orderId);
		depositOrder.setOrderStatus(orderStatus);
		depositOrder.setOrderDesc(orderDesc);
		depositOrder.setModifyTime(new Date());
		depositOrderMapper.updateByOrderId(depositOrder);
	}
}